package com.android.kotlincoroutinemvvmdemo.dialog
import android.app.Dialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.res.Configuration
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import com.android.kotlincoroutinemvvmdemo.Constants
import com.android.kotlincoroutinemvvmdemo.CoroutineMVVMApp

//onCreateDialog返回Dialog,通常用于替代传统的Dialog对话框的场景.
abstract class BaseDialogFragment<T : ViewDataBinding> : DialogFragment() {

    protected lateinit var mBinding : T

    private var mReceiver : AdapterUIModeNightBroadcastReceiver? = null

    private val mIntentFilter by lazy {
        IntentFilter().apply {
            addAction(Constants.DAY_NIGHT_UI_MODE_CHANGE)
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireContext(), 0)
        //设置不可取消
        //dialog.setCancelable(false)
        //dialog.setCanceledOnTouchOutside(false)
        dialog.window?.apply {
            decorView.background = ColorDrawable(0x00000000)
            val mAttribute = attributes
            //通过WindowManager.LayoutParams设置宽高
            //mAttribute.width = WindowManager.LayoutParams.WRAP_CONTENT
            //mAttribute.height = WindowManager.LayoutParams.WRAP_CONTENT
            mAttribute.gravity = Gravity.CENTER
            attributes = mAttribute
        }
        mBinding =
            DataBindingUtil.inflate(LayoutInflater.from(requireContext()), getLayoutId(), null, false)
        init(savedInstanceState)
        dialog.setContentView(mBinding.root)
        return dialog
    }

    override fun onStart() {
        super.onStart()
        mReceiver = AdapterUIModeNightBroadcastReceiver()
        requireContext().registerReceiver(mReceiver, mIntentFilter)
        adapterUIModeNightForDialog(CoroutineMVVMApp.instance!!.isDarkTheme())
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        //用来处理屏幕旋转
    }

    override fun onStop() {
        super.onStop()
        requireContext().unregisterReceiver(mReceiver)
        mReceiver = null
    }

    abstract fun getLayoutId(): Int

    abstract fun init(savedInstanceState: Bundle?)

    open fun adapterUIModeNightForDialog(uiModeNight: Boolean) {}

    inner class AdapterUIModeNightBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (context == null || intent == null) {
                return
            }
            when (intent.action) {
                Constants.DAY_NIGHT_UI_MODE_CHANGE -> {
                    val dayNightYes = intent.getBooleanExtra(Constants.DAY_NIGHT_FLAG, false)
                    adapterUIModeNightForDialog(dayNightYes)
                }
            }
        }
    }

}